import { reqGetCode, reqUserRegister, reqUserLogin, reqGetUserInfo, reqUserLogout ,reqUserAddressList} from '@/api'
import { getUserTempId } from '@/uitls/userAbout'

const state = {
    //  初始化的时候就获取用户的临时标识  存储到这
    userTempId: getUserTempId(),
    code: '',
    // token: '',  //  初始化为空串我们是做登录 
    // token:localStorage.getItem('TOKEN_KEY'),
    token:localStorage.getItem('TOKEN_KEY'),
    userInfo:{},
    userAddressList:[]
}
const mutations = {
    RECEIVE_CODE(state, code) {
        state.code = code
    },

    RECEIVE_TOKEN(state, token) {
        state.token = token
    },
    RECEIVE_USERINFO(state, userInfo) {
        state.userInfo = userInfo
    },

    RESET_TOKEN(state){
        state.token = ''
        state.userInfo = {}
    },

    RECEIVE_USERADDRESSLIST(state,userAddressList){
        state.userAddressList = userAddressList
    }
}   
const actions = {
    // 获取验证码
    async getCode({ commit }, phone) {
        const result = await reqGetCode(phone)
        if (result.code === 200) {
            commit('RECEIVE_CODE', result.data)
            return result.data
        } else {
            return Promise.reject(new Error('failed'))
        }
    },
    // 注册
    async userRegister({ commit }, userInfo) {
        const result = await reqUserRegister(userInfo)
        if (result.code === 200) {
            return 'ok'
        } else {
            return Promise.reject(new Error('failed'))
        }
    },

    // 登录
    async userLogin({ commit }, userInfo) {
        const result = await reqUserLogin(userInfo)
        if (result.code === 200) {
            commit('RECEIVE_TOKEN', result.data.token)
            // 要想自动登录  这里拿到token必须往localStorage也存一份
            // localStorage.setItem('TOKEN_KEY',result.data.token)
            localStorage.setItem('TOKEN_KEY',result.data.token)
            return 'ok'
        } else {
            return Promise.reject(new Error('failed'))
        }
    },
    // 获取用户信息
    async getUserInfo({ commit }) {
        const result =await reqGetUserInfo()
        if (result.code === 200) {
            commit('RECEIVE_USERINFO',result.data)
            return 'ok'
        } else {
            return Promise.reject(new Error('failed'))
        }
    },
    //  清空token
    async resetToken({commit}){
        commit('RESET_TOKEN')  // 清空state内部的token
        localStorage.removeItem('TOKEN_KEY')  //  清空localStorage内部的token
    },

    // 退出登录
    async userLogout({commit,dispatch}){
        const result =await reqUserLogout()
        if(result.code === 200){
            dispatch('resetToken')  
            // 退出登录成功 清空当前state中的token和用户信息  还有localStorage中的token
            return 'ok'
        }else{
            return Promise.reject(new Error('failed'))
        }
    },
    //  获取用户地址
    async getUserAddressList({commit}){
        const result =await reqUserAddressList()
        if(result.code === 200){
            commit('RECEIVE_USERADDRESSLIST',result.data)
        }
    }

}
const getters = {}

export default {
    state,
    mutations,
    actions,
    getters,
}